package im.yisme.view.activities;

import im.yisme.R;
import im.yisme.view.service.ViewService;
import im.yisme.view.service.ViewServiceProvider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class PlayLoadActivity extends Activity {
	private Handler handler;
	private int count;
	private ViewService viewCtrl;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_palyload);		
		setProgressBarVisibility(true);
		
		viewCtrl=ViewServiceProvider.getViewService();
		
		final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
		handler = new Handler(){
			public void handleMessage(Message msg) {
				if(msg.what == 0x111){
					progressBar.setProgress(count);
				}
			}
		};

		load();
	}
	
	
	private void load(){
		new Thread(new Runnable() {
			public void run() {
				while(count < 100){
					Message message = new Message();
					message.what = 0x111;
					++ count;
					handler.sendMessage(message);
					try {
						Thread.sleep(30);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				jumpToGame();
			}
		}).start();
	}
	
	private void jumpToGame(){
		Intent intent = new Intent(this,GameActivity.class);
		Intent preIntent = getIntent();
		
		intent.putExtra("musicVO", preIntent.getSerializableExtra("musicVO"));
		intent.putExtra("lyricsVO", preIntent.getSerializableExtra("lyricsVO"));
		viewCtrl.jumpTo(intent);
	}
	
	
	protected void onResume(){
		super.onResume();
		viewCtrl.updateTopActivity(this);
	}
}
